package com.kedll.library.base;

import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.kedll.library.KBaseActivity;
import com.kedll.library.R;
import com.kedll.library.listener.OnClickListener;
import com.kedll.library.mvp.BasePresenter;

/**
 * 包含Toolbar的Activity可以直接继承此类，使用方式的伪代码如下：
 * <p>
 * activity.xml文件
 * <LinearLayout>
 * <p>
 * ...
 * <include layout="@layout/include_toolbar_layout" />
 * ...
 * </LinearLayout>
 * <p>
 * Activity.class文件
 * public class MainActivity extends MyBaseFragmentActivity{
 * ...
 * protected void initToolBar(Bundle bundle){
 * super.initToolBar(bundle);//默认实现了返回上一级功能，如果不需要，则可以完全重写此类
 * setTitle("demo");//设置标题
 * <p>
 * setTopLeftButton(R.drawable.back_img, new OnClickListener(){//设置toolbar左边按钮的图标和点击事件
 * public void onClick(){
 * //TODO 实现业务逻辑
 * }
 * });
 * <p>
 * setTopRightButton("RightBtn", R.drawable.right, new OnClickListener(){//设置toolbar右边按钮的图标和点击事件
 * public void onClick(){
 * //TODO 实现业务逻辑
 * }
 * });
 * }
 * ...
 * }
 *
 * @param <V>
 */
public abstract class MyBaseFragmentActivity<V extends ViewDataBinding, T extends BasePresenter> extends KBaseActivity<V> {

    protected Toolbar toolbar;
    protected TextView tvTitle;
    protected AppBarLayout appBarLayout;
    protected int menuResId;
    protected String menuStr;
    protected T mPresenter;
    private OnClickListener onClickListenerTopLeft;
    private OnClickListener onClickListenerTopRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    @CallSuper
    protected void initData(Bundle savedInstanceState) {
        toolbar = findViewById(R.id.toolbar);
        tvTitle = findViewById(R.id.title);
        appBarLayout = findViewById(R.id.appBarLayout);

        if (toolbar != null) {
            setSupportActionBar(toolbar);
            if (getSupportActionBar() != null) {
                getSupportActionBar().setDisplayShowTitleEnabled(false);
            }
            initToolBar(savedInstanceState);
        }
        mPresenter = getPresenter();
    }

    /**
     * 默认设置左边按钮为返回
     *
     * @param savedInstanceState
     */
    protected void initToolBar(Bundle savedInstanceState) {
        setTopLeftButton(R.drawable.ic_arrow_back, new OnClickListener() {
            @Override
            public void onClick(View v) {
                MyBaseFragmentActivity.this.finish();
            }
        });
    }

    protected abstract T getPresenter();

    /**
     * 设置toolbar左边图标和点击事件
     *
     * @param iconResId            图标资源
     * @param clickListenerTopLeft 点击回调
     */
    protected void setTopLeftButton(int iconResId, OnClickListener clickListenerTopLeft) {
        if (iconResId != 0) {
            toolbar.setNavigationIcon(iconResId);
        }
        this.onClickListenerTopLeft = clickListenerTopLeft;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onAttach(this);
        }
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.onDetach();
        }
        super.onDestroy();
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
//            toolbar.setTitle(title);
        }

    }

    /**
     * 设置toolbar右边图标和点击事件
     *
     * @param menuStr                 按钮名称
     * @param onClickListenerTopRight 点击回调
     */
    protected void setTopRightButton(String menuStr, OnClickListener onClickListenerTopRight) {
        this.menuStr = menuStr;
        this.onClickListenerTopRight = onClickListenerTopRight;
    }

    /**
     * 设置toolbar右边图标和点击事件
     *
     * @param menuStr                 按钮名称
     * @param menuResId               图标资源
     * @param onClickListenerTopRight 点击回调
     */
    protected void setTopRightButton(String menuStr, int menuResId, OnClickListener onClickListenerTopRight) {
        this.menuStr = menuStr;
        this.menuResId = menuResId;
        this.onClickListenerTopRight = onClickListenerTopRight;
    }

    /**
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_activity_base_top_bar, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (menuResId != 0) {
            menu.findItem(R.id.menu_1).setIcon(menuResId);
        }

        if (!TextUtils.isEmpty(menuStr)) {
            menu.findItem(R.id.menu_1).setTitle(menuStr);
        }

        if (menuResId == 0 && TextUtils.isEmpty(menuStr) && menu.findItem(R.id.menu_1) != null) {
            menu.findItem(R.id.menu_1).setVisible(false);
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (onClickListenerTopLeft != null) {
                onClickListenerTopLeft.onClick(toolbar);
            }
            return true;
        } else if (item.getItemId() == R.id.menu_1) {
            if (onClickListenerTopRight != null) {
                onClickListenerTopRight.onClick(toolbar);
            }
            return true;
        }
        return false;
    }

    /**
     * 拉起系统电话
     */
//    protected void startPhone(String uri) {
//        if (uri == null || "".equals(uri)) {
//            return;
//        }
//        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + uri));
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
//    }

    /**
     * 跳转到登录页面
     */
//    protected void startLoginActivity(int requesCode) {
//        utils.showToast(getApplicationContext(),
//                getString(R.string.please_login_zh_));
//        startActivityForResult(new Intent(getApplicationContext(),
//                        LoginFragmentActivity.class),
//                requesCode);
//        overridePendingTransition(R.anim.bottom_in, R.anim.no_animation);
//    }


}
